pp108 : Sprintf Function for Formatting Time

Sprintf Function for Formatting Time

This topic describes the sprintf function for formatting time.


You can use the sprintf function to customize the time format such that the time is displayed in either the 12-hr format or the 24-hr format.

The following table lists the formats supported by the sprintf function for formatting time:

Table 1. Sprintf Function for Formatting Time

Format

Description

%H

Hour in the 24-hour clock represented by a number between 0 and 23

%HH

Hour in the 24-hour clock represented by a number between 00 and 23

%h

Hour in the 12-hour clock represented by a number between 1 and 12

%hh

Hour in the 12-hour clock represented by a number between 01 and 12

%m

Minutes represented by a number between 0 and 59

%mm

Minutes represented by a number between 00 and 59

%s

Seconds represented by a number between 0 and 59

%ss

Seconds represented by a number between 00 and 59

%t

A or P (in user's locale) respectively for ante-meridian and post-meridian

%tt

AM or PM respectively for ante-meridian and post-meridian



Note: The sprintf function for formatting time can be used in the following ways:

  • sprintf("%T(format)", UTCTime)

  • sprintf("%XT(format)", CordysDateFormat)


    The following are the examples of the output generated by various combinations of this function when the locale is Asia (GMT+5.30):

  • sprintf("%T(%HH:%mm:%ss)",0)will display the time as 05:30:00
  • sprintf("%T(%HH:%mm:%ss %tt)",0)will display the time as 05:30:00 AM
  • sprintf("%XT(%HH:%mm:%ss %tt)","2008-01-01T00:00:01" )will display the time as 00:00:01 AM
  • sprintf("%T(%HH:%mm:%ss %tt)",utc())will display the current locale time as 00:00:01 AM
  • sprintf("%T(%HH:%mm:%ss %tt)",utcgmt())will display the current GMT time as 06:30:00 PM